package com.summer.thread;

import java.util.concurrent.Exchanger;

public class ExchangerTest {
	public static void main(String[] args) {
		Exchanger<String> exchanger = new Exchanger<String>();
		ExchangerThread task = new ExchangerThread(exchanger);
		task.start();
		try {
			String msg = exchanger.exchange("主线程：B");
			System.out.println("主线程 收到消息："+msg);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static class ExchangerThread extends Thread{
		private Exchanger<String> exchanger;
		ExchangerThread(Exchanger<String> exchanger){
			this.exchanger = exchanger;
		}
		
		@Override
		public void run() {
			try {
				String msg = currentThread().getName()+"：A";
				msg = exchanger.exchange(msg);
				System.out.println(currentThread().getName()+"收到消息:"+msg);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
